<?php
namespace app\admin\controller;
use think\Db;
use think\Request;

class Technmove extends Base
{
	public function _initialize()
	{
		// 调用父类前置方法
		parent::_initialize();

		// 登录校验
		$this->IsLogin();
	}

    public static function move_status($status = null){
        $sta = [
            0=>'待审核',
            1=>'审核通过',
            2=>'申请驳回',
        ];
        if ($status >= 0){
            return $sta[$status];
        }else{
            return $sta;
        }
    }
	/**
	 * 技师等级列表
	 */
	public function index(Request $request)
	{
        if($request->isAjax()){

            $list = Db::name('techn_move')->order('id desc')->select();
            $count = Db::name('techn_move')->order('id desc')->count();
            if (!empty($list)){
                foreach ($list as $kk => $vv){
                    $list[$kk]['techn_name'] = Db::name('techn')->where('id',$vv['techn_id'])->value('nickname');
                    $list[$kk]['add_time_time'] = date('Y-m-d H:i',$vv['add_time']);
                    $list[$kk]['upd_time_time'] =  $vv['upd_time'] > 0 ? date('Y-m-d H:i',$vv['upd_time']) : 0;
                    $list[$kk]['old'] =  Db::name('area')->where('id',$vv['old_qu'])->value('merger_name');
                    $list[$kk]['new'] =  Db::name('area')->where('id',$vv['qu'])->value('merger_name');
                    $list[$kk]['status_s'] = self::move_status($vv['status']) ;

                }
            }
            $return['code'] = 0;
            $return['msg'] = '获取成功';
            $return['data'] = $list;
            $return['count'] = $count;
            echo json_encode($return);
        }else{
            return $this->fetch();
        }


	}

    public function agree(Request $request){
        if ($request->isAjax()){
            $param = $request->param();
            $info = Db::name('techn_move')->where('id',$param['id'])->find();
            $n_sheng = Db::name('area')->where('id',$info['sheng'])->value('name');
            $n_shi = Db::name('area')->where('id',$info['shi'])->value('name');
            $n_qu = Db::name('area')->where('id',$info['qu'])->value('name');
            $updata = [
                'province'=>$info['sheng'],
                'city'=>$info['shi'],
                'county'=>$info['qu'],
                'upd_time'=>time(),
                'province_name'=>$n_sheng,
                'city_name'=>$n_shi,
                'county_name'=>$n_qu,
            ];
            $re = Db::name('techn')->where('id',$info['techn_id'])->update($updata);
            if ($re){
                Db::name('techn_move')->where('id',$param['id'])->update(['status'=>1]);
                echo json_encode(['code'=>0,'msg'=>'审核通过']);
            }else{
                echo json_encode(['code'=>-1,'msg'=>'审核失败，请重试']);
            }
        }
    }

    public function disagree(Request $request){
        if ($request->isAjax()){
            $param = $request->param();
                $re = Db::name('techn_move')->where('id',$param['id'])->update(['status'=>2]);
                if($re){
                    echo json_encode(['code'=>0,'msg'=>'驳回成功']);
                }else{
                    echo json_encode(['code'=>-1,'msg'=>'请重试']);
                }

        }
    }
}